﻿-'use strict';
app.controller('PinManageController',
    [
    '$rootScope',
    '$scope',
    '$stateParams',
    '$localStorage',
    '$state',
    '$modal',
    'PinManageFactory',
    function ($rootScope, $scope, $stateParams, $localStorage, $state, $modal, PinManageFactory) {             
        $scope.title = "Pin Manage",
        $scope.messeger = "",
        $scope.ListCard = [],
        $scope.ListHistory = [],
        $scope.popup = function (windowClass, StyleModal, Controller, size) {
            $modal.open({
                windowClass: windowClass,
                templateUrl: StyleModal,
                controller: Controller,
                size: size,
                resolve: {
                    messeger: function () {
                        return $scope.messeger;
                    },
                    title: function () {
                        return $scope.title;
                    }
                }
            })
        },
        getList($localStorage.user.UserId);
        GetPinHistory($localStorage.user.UserId);
        function getList(UserId) {
            PinManageFactory.GetListCard(UserId).then(
                function successCallback(resp) {
                    $scope.ListCard = resp.data.Result;
                    $scope.totalItems = $scope.ListCard.length;
                    $scope.itemsPerPage = 2;
                    $scope.currentPage = 1;

                    $scope.setPage = function (pageNo) {
                        $scope.currentPage = pageNo;
                    };

                    $scope.pageChanged = function () {
                        $log.log('Page changed to: ' + $scope.currentPage);
                    };

                    $scope.maxSize = 150;
                    $scope.bigTotalItems = 5000;
                    $scope.bigCurrentPage = 1;

                }, function errorCallback(resp) {
                    $scope.messeger = "";
                    console.log(resp)
                })
        };
        function GetPinHistory(UserId) {
            PinManageFactory.getPinHistory(UserId).then(
                function successCallback(resp)
                {
                    if (resp.data.response.length > 0)
                    {
                        $scope.ListHistory = resp.data.response;
                        $scope.total = $scope.ListHistory.length;
                        $scope.PerPage = 12;
                        $scope.current = 1;

                        $scope.setPage = function (pageNum) {
                            $scope.current = pageNum;
                        };

                        $scope.pgChanged = function () {
                            $log.log('Page changed to: ' + $scope.current);
                        };

                        $scope.maxPin = 10000;
                    }

                }, function errorCallback(resp) {
                    $scope.messeger = "";
                    console.log(resp)
                })
        }
    }
]);
app.controller('PinAddModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $state, $stateParams, $timeout, messeger, title, commonFactory, PinManageFactory, BitcoinFactory) {
    $scope.messeger = messeger,
    $scope.title = "Add",
    $scope.AlertType = "warning";
    $scope.processing = false,
    $scope.showQRCode = false,
    $scope.wallet = {},
    $scope.Rate = {},
    $scope.total = 0,
    $scope.BitcoinRate = 0,
    $scope.WalletList = [];
    $scope.doAddPin = function (item) {
        var btc = parseFloat(item.totalBtc).toFixed(8);
        if (item.Quantum > 0 && item.PaymentId > 0 && item.Quantum >= 4)
        {
            $state.go("app.pinpayment", { id: item.PaymentId, amount: btc })
            $modalInstance.close();
        } else {
            $scope.messeger = "Amount field required and must be greater than 4";
            $scope.processing = false;
        }
    }
    $scope.doTotal = function (quantum) {
        if (quantum > 0) {
            $scope.total = quantum * $scope.Rate.Value;
        } else {
            $scope.total = 0;
        }
    }
    GetPinRate(),
    GetBitcointoUSDRate(1);
    function GetPinRate() {
        commonFactory.GetPinRate().then(function (response) {
            $scope.Rate = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    }
    function GetBitcointoUSDRate(Bitcoin) {
        BitcoinFactory.GetBitcointoUSDRate(Bitcoin).then(function (response) {
            $scope.BitcoinRate = response.data;            
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    }
  
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
app.controller('PinTransferModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $state, $stateParams, $timeout, messeger, title, commonFactory, PinManageFactory) {
    $scope.messeger = messeger,
    $scope.title = "Transfer Pin",
    $scope.AlertType = "warning";
    $scope.processing = false,
    $scope.showQRCode = false,
    $scope.selected = [],
    $scope.ddluser = {},
    $scope.ListUser = [],
    $scope.doTransfer = function (data) {
        $scope.processing = true;
        if (data.UserId > 0 && data.Quantum > 0 && data.Password2 != "") {
            Transfer(data);
        } else {
            $scope.messeger = "Recipient,Quantum and Password 2 field required.";
        }
    };
    GetListUser($localStorage.user.UserId),
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    function GetListUser(id) {
        commonFactory.getListUser(id).then(function (rsp) {
            if (rsp.data.Status) {
                $scope.ListUser = rsp.data.Result;
            } else {
                $scope.messeger = rsp.data.Msg;             
            }
        });
    };
    function Transfer(data) {
        PinManageFactory.transferPinOfQuantum($localStorage.user.UserId, data.UserId, data.Quantum, data.Password2).then(function (rsp) {
            if (rsp.data.Status) {
                $scope.messeger = rsp.data.Msg;
                setTimeout(location.reload(), 6000);
            } else {
                $scope.processing = false;
                $scope.messeger = rsp.data.Msg;               
            }
        });
    }
});